package cn.edu.sanxiau.www.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.sanxiau.www.model.Role;
import cn.edu.sanxiau.www.model.User;
import cn.edu.sanxiau.www.model.UserRole;
import cn.edu.sanxiau.www.servier.UserRoleService;
import frame.controller.BaseController2;

@WebServlet(urlPatterns = "/roleController.do")
public class RoleController extends BaseController2 {

    /**
     *
     */
    private static final long serialVersionUID = -8239164249302224549L;

    //列表
    public String listRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        //3.调用业务；
        List<Role> roleList_db = roleService.queryAllRole();
        for (Role role_db : roleList_db) {
            System.out.println("--------listRole--------" + role_db);
        }
        //响应这个数据
        req.setAttribute("roleList_db", roleList_db);
        //2.跳转页面；
        return "view/system/role/roleList.jsp";

    }

    // 增加
    public String addRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String name = req.getParameter("name");
        String state = req.getParameter("state");
        String description = req.getParameter("description");

        //封装
        Role role = new Role();
        role.setName(name);
        role.setState(state);
        role.setDescription(description);

        System.out.println("-----roleController---addrole--------" + role);

        //3.调用业务；
        int n = roleService.addRoleByRole(role);

        //2.跳转页面；
        //响应这个数据
        List<Role> roleList_db = roleService.queryAllRole();
        req.setAttribute("roleList_db", roleList_db);
        return "view/system/role/roleList.jsp";
    }


    //删除
    public String deleteRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String role_idStr = req.getParameter("role_idStr");
        //字符转数字类型
        int role_id = Integer.parseInt(role_idStr);
        System.out.println("--------role_id--------" + role_id);

        //3.调用业务；
        int n = roleService.deleteRoleByRole_id(role_id);


        //2.跳转页面；
        //响应这个数据
        List<Role> roleList_db = roleService.queryAllRole();
        req.setAttribute("roleList_db", roleList_db);
        return "view/system/role/roleList.jsp";

    }

    //修改UI
    public String updateUIRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String role_idStr = req.getParameter("role_idStr");
        //字符转数字类型
        int role_id = Integer.parseInt(role_idStr);


        //3.调用业务；
        Role role_db = roleService.queryRoleByRole_id(role_id);
        System.out.println("--------role_db--------" + role_db);
        req.setAttribute("role", role_db);

        //2.跳转页面；
        return "view/system/role/updateRole.jsp";

    }

    //修改
    public String updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；

        String user_idStr = req.getParameter("user_idStr");
        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);

        String name = req.getParameter("name");
        String sex = req.getParameter("state");
        String address = req.getParameter("description");


        //封装
        Role role = new Role();
        role.setRole_id(user_id);
        role.setName(name);
        role.setState(sex);
        role.setDescription(address);


        System.err.println("------------------updateUser------------" + role);


        //3.调用业务；
        int n = roleService.updateRoleByRole(role);

        //2.跳转页面；
        //响应这个数据
        List<Role> roleList_db = roleService.queryAllRole();
        req.setAttribute("roleList_db", roleList_db);
        return "view/system/role/roleList.jsp";

    }

    //分配角色UI
    public String FPRoleUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String user_idStr = req.getParameter("user_idStr");
        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);
        System.err.println("------------------FPRoleUI()------------" + user_id);

        //3.调用业务；
        //a.给那个用户分配
        User user_db = userServier.queryUserByUser_id(user_id);
        req.setAttribute("user", user_db);

        //b.所有角色数据
        List<Role> roleList_db = roleService.queryAllRole();
        req.setAttribute("roleList_db", roleList_db);

        //d.选择角色数据
        Role XZRole = roleService.queryXZRoleByUserId(user_id);
        req.setAttribute("XZRole", XZRole);


        //2.跳转页面；
        return "view/system/role/FPRole.jsp";

    }

    //分配角色
    public String FPRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String user_idStr = req.getParameter("user_idStr");
        String role_idSTR = req.getParameter("role_idSTR");

        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);
        int role_id = Integer.parseInt(role_idSTR);


        //3.调用业务；
        //封装
        UserRole userRole = new UserRole();
        userRole.setUser_id(user_id);
        userRole.setRole_id(role_id);
        //先删除，判断数据库里面有没有
        int m = userRoleService.deleteUserRoleByUser_id(user_id);
        //业务增加
        int n = userRoleService.addUserRoleByUserRole(userRole);


        //2.跳转页面；
        //a.给那个用户分配
        User user_db = userServier.queryUserByUser_id(user_id);
        req.setAttribute("user", user_db);

        //b.所有角色数据
        List<Role> roleList_db = roleService.queryAllRole();
        req.setAttribute("roleList_db", roleList_db);

        //c.友情提示数据
        req.setAttribute("message", "分配角色成功！！！");

        //d.选择角色数据
        Role XZRole = roleService.queryXZRoleByUserId(user_id);
        req.setAttribute("XZRole", XZRole);


        return "view/system/role/FPRole.jsp";

    }


}
